
Imports System.Collections.Specialized.NameObjectCollectionBase
Imports System.Web.UI
Imports CasClientBase
Imports ccapps.persistent.blackBerry_JOB
Imports System.Diagnostics.Debug
Imports ccapps.model.cas

Namespace batchjobs
    Public Class JOB_BB
        Private errstr As String = ""

        Public Shared Function BB_Update(ByVal requestno As String) As String
            Dim bb_job As New JOB_BB 'TEMPORARY FOR TEST
            Dim s As New CasClientBase.Class1
            Dim giris As String
            Dim myrow As DataRow
            Dim username As String
            Try
                giris = s.Login("10204", "lhssert01")

                If giris <> "1" Then
                    s.Send("[CANCEL()]")
                    s.Logout()
                    Return giris
                End If

                Dim temp As DataTable
                Dim temp1 As DataTable
                Dim spcode As Int16
                Dim coid As Int64
                Dim msisdn As Int64
                Dim internal_id As Int64
                Dim dat1 As Int64 = 0
                Dim den As String = Nothing
                Dim sncode As Int16 = 53 '18

                Dim status_addservice As Boolean
                Dim status_commit As Boolean
                Dim status_addserviceprm As Boolean


                Dim cs As New ContractService_lite
                Dim cspr As New Contr_Serv_Prm


                
                temp = getBBCust()
                dat1 = temp.Rows.Count

                Dim without_bundle As Byte = 0
                If without_bundle = 1 Then 'CHECKS OFFER TYPE. IF BUNDLED OR NOT
                    'STATEMENT TO ASSIGN BB MONTHLY FEE SERVICE

                    'SERVICE PARAMETERS FOR BB-TEST SERVICE SNCODE=187 
                    cs.cos_action = "a"
                    cs.cos_sn_code = "187"
                    cs.cos_sp_code = "6"
                    cs.cosc_num = "0"
                    cs.cosc_action = "0"

                    For Each myrow In temp.Rows
                        coid = myrow(0)
                        msisdn = myrow(1)
                        internal_id = myrow(2)
                        den = den & " " & Convert.ToString(myrow(0))
                        temp1 = getSPcode(Convert.ToString(sncode))

                        If temp1.Rows.Count = 1 Then
                            spcode = temp1.Rows(0).Item(0)
                            status_addservice = bb_job.addService(coid, s, cs) 'TEMPORARY
                            status_commit = bb_job.commit_contract(coid, s)
                        Else
                            '
                        End If
                    Next
                Else
                    'STATEMENT TO ASSIGN COFU AND FREE UNIT SERVICE
                    Dim chk_status As Boolean

                    'SERVICE PARAMETERS FOR COFU SNCODE=53 
                    cs.cos_action = "a"
                    cs.cos_sn_code = "53"
                    cs.cos_sp_code = "6"
                    cs.cos_quantity = "1"
                    cs.cosc_num = "0"
                    cs.cosc_action = "0"
                    'SERVICE PARAMETER FOR PARAMETER_VALUE
                    cspr.cos_action = "a"
                    cspr.cos_spcode = "6"
                    cspr.cos_sncode = "53"
                    cspr.prm_no = "1"
                    cspr.prm_id = "34"
                    cspr.multi_values = "1"
                    cspr.value_seqno = "1"
                    cspr.value = "2"


                    For Each myrow In temp.Rows
                        coid = myrow(0)
                        msisdn = myrow(1)
                        internal_id = myrow(2)
                        den = den & " " & Convert.ToString(myrow(0))
                        temp1 = getSPcode(Convert.ToString(sncode))

                        If temp1.Rows.Count = 1 Then
                            spcode = temp1.Rows(0).Item(0)
                            chk_status = bb_job.checkService(coid, spcode, sncode, s) 'TEMPORARY
                            If chk_status = False Then
                                'ADD SERVICE

                                status_addservice = bb_job.addService(coid, s, cs) 'TEMPORARY
                                status_addserviceprm = bb_job.addServicePrm(coid, s, cspr)
                                status_commit = bb_job.commit_contract(coid, s)
                                If status_commit Then setLogStatus(internal_id, msisdn, 1)
                            Else
                                'SERVICE ALREADY EXISTS 
                            End If
                        Else
                            '
                        End If
                    Next
                End If

                dat1 = dat1 + 1
            Catch ex As Exception
                Console.Write(ex)
            End Try
        End Function
        Function isError(ByVal inputResult As String, ByVal param1 As String, ByVal param2 As String, ByVal s As CasClientBase.Class1) As Integer
            If inputResult.IndexOf("ERROR") > 0 Or inputResult.IndexOf("RC0000") < 0 Then
                errstr += param1 + "     " + inputResult.Substring(0, inputResult.IndexOf("]") + 1) + vbNewLine
                s.Send("[CANCEL()]")
                Return 1
            End If
            Return 0

        End Function

        Function addService(ByVal coid As Int64, ByVal s As CasClientBase.Class1, ByVal cs As model.cas.ContractService_lite) As Boolean
            Dim status As Boolean = False
            Dim dd1 As String = s.Send("[CONTRACT.EXISTS("""",""" & Convert.ToString(coid) & """)]")

            If isError(dd1, "coid:" + Convert.ToString(coid), "", s) = 1 Then
                status = False
            Else
                Dim dd2 As String = s.Send(cs.Write) 'CONTRSERVICES.WRITE
                If isError(dd2, "coid:" + Convert.ToString(coid), "", s) = 1 Then
                    status = False
                    s.Send("[CANCEL()]")
                Else : status = True
                End If
            End If

            Return status
        End Function
        Function addServicePrm(ByVal coid As Int64, ByVal s As CasClientBase.Class1, ByVal cspr As model.cas.Contr_Serv_Prm) As Boolean
            Dim status As Boolean = False
            Dim i As String = cspr.Write
            Dim dd2 As String = s.Send(cspr.Write) 'CONTRSERVICES.WRITE
            If isError(dd2, "coid:" + Convert.ToString(coid), "", s) = 1 Then
                status = False
                s.Send("[CANCEL()]")
            Else : status = True
            End If
            Return status
        End Function
        Function commit_contract(ByVal coid As Int64, ByVal s As CasClientBase.Class1)
            Dim status As Boolean = True
            Dim commitContr As String = s.Send("[COMMIT_CONTRACT(""1"","""","""","""","""")]")

            If isError(commitContr, "coid:" + Convert.ToString(coid), "", s) <> 1 Then
                status = True
            Else : status = False
                s.Send("[CANCEL()]")
            End If

            Return status
        End Function
        Function checkService(ByVal coid As Int64, ByVal spcode As Int16, ByVal sncode As Int16, ByVal s As CasClientBase.Class1) As Boolean
            Dim dd1 As String = s.Send("[CONTRACT.EXISTS("""",""" & Convert.ToString(coid) & """)]")
            Dim err As Integer = 0

            If isError(dd1, "coid:" + Convert.ToString(coid), "", s) = 1 Then
                err = 1
            Else
                err = 0
                Dim t As String = "[CONTRACTSERVICE.READ(""" & coid & """,""" & spcode & """,""" & sncode & """)]"
                Dim dd2 As String = s.Send("[CONTRACTSERVICE.READ(""" & coid & """,""" & spcode & """,""" & sncode & """)]")
                If isError(dd2, "coid:" + Convert.ToString(coid), "", s) = 1 Then
                    Return False
                Else
                    Return True
                End If
            End If
        End Function


        Function LogError()

        End Function

    End Class
End Namespace
